package com.localjoycommunity.cpdj.project.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.localjoycommunity.cpdj.common.utils.DateUtils;
import com.localjoycommunity.cpdj.common.utils.StringUtils;
import com.localjoycommunity.cpdj.project.system.domain.SysOption;
import com.localjoycommunity.cpdj.project.system.domain.SysQuestion;
import com.localjoycommunity.cpdj.project.system.domain.vo.OptionVo;
import com.localjoycommunity.cpdj.project.system.domain.vo.QuestionVo;
import com.localjoycommunity.cpdj.project.system.mapper.SysOptionMapper;
import com.localjoycommunity.cpdj.project.system.mapper.SysPaperQuestionMapper;
import com.localjoycommunity.cpdj.project.system.mapper.SysQuestionMapper;
import com.localjoycommunity.cpdj.project.system.service.ISysQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 试题Service业务层处理
 *
 * @author leepandar
 * @date 2020-11-01
 */
@Service
public class SysQuestionServiceImpl implements ISysQuestionService {
    @Autowired
    private SysQuestionMapper sysQuestionMapper;
    @Autowired
    private SysPaperQuestionMapper paperQuestionMapper;
    @Autowired
    private SysOptionMapper sysOptionMapper;

    /**
     * 查询试题
     *
     * @param questionId 试题ID
     * @return 试题
     */
    @Override
    public SysQuestion selectSysQuestionById(Long questionId) {
        SysQuestion sysQuestion = sysQuestionMapper.selectSysQuestionById(questionId);
//        Map<String, Object> map = new HashMap<>();
//        map.put("options", this.sysOptionMapper.selectOptionByQustionId(questionId));
//        sysQuestion.setParams(map);
        return sysQuestion;
    }

    /**
     * 查询试题列表
     *
     * @param sysQuestion 试题
     * @return 试题
     */
    @Override
    public List<SysQuestion> selectSysQuestionList(SysQuestion sysQuestion) {
        return sysQuestionMapper.selectSysQuestionList(sysQuestion);
    }

    @Override
    public List<QuestionVo> selectQuestions(long paperId) {
        return sysQuestionMapper.selectQuestions(paperId);
    }

    @Override
    public List<OptionVo> selectOptions(long questionId) {
        return sysQuestionMapper.selectOptions(questionId);
    }

    /**
     * 新增试题
     *
     * @param sysQuestion 试题
     * @return 结果
     */
    @Override
    public int insertSysQuestion(SysQuestion sysQuestion) {
        sysQuestion.setCreateTime(DateUtils.getNowDate());
        sysQuestion.setUpdateTime(DateUtils.getNowDate());
        sysQuestionMapper.insertSysQuestion(sysQuestion);
        Long questionId = sysQuestion.getQuestionId();
        List<SysOption> options = sysQuestion.getOptions();
        String correctOption ="";
//        List<SysOption> options = (List<SysOption>) sysQuestion.getParams().get("options");
        for (int i = 0; i < options.size(); i++) {
            SysOption option = JSON.parseObject(JSON.toJSONString(options.get(i)), SysOption.class);
            option.setCreateTime(DateUtils.getNowDate());
            option.setQuestionId(questionId);
            sysOptionMapper.insertSysOption(option);
            if (option.getCorrect() == 1) {
                correctOption += option.getOptionId() + ",";
            }
        }
        if (StringUtils.isNotBlank(correctOption)) {
            correctOption = correctOption.substring(0, correctOption.length() - 1);
        }
        sysQuestion.setCorrectOption(correctOption);
        return sysQuestionMapper.updateSysQuestion(sysQuestion);
    }

    /**
     * 修改试题
     *
     * @param sysQuestion 试题
     * @return 结果
     */
    @Override
    public int updateSysQuestion(SysQuestion sysQuestion) {
        sysQuestion.setUpdateTime(DateUtils.getNowDate());
        /*List<SysOption> options = sysQuestion.getOptions();
        if (options != null && options.size() > 0) {
            //删除所有再新增
            sysOptionMapper.deleteSysOptionById(sysQuestion.getQuestionId());
            for (SysOption option : options) {
                option.setQuestionId(sysQuestion.getQuestionId());
                option.setCreateTime(DateUtils.getNowDate());
                sysOptionMapper.insertSysOption(option);
            }
        }*/
        return sysQuestionMapper.updateSysQuestion(sysQuestion);
    }

    /**
     * 批量删除试题
     *
     * @param questionIds 需要删除的试题ID
     * @return 结果
     */
    @Override
    public int deleteSysQuestionByIds(Long[] questionIds) {
        sysOptionMapper.deleteSysOptionByIds(questionIds);
        return sysQuestionMapper.deleteSysQuestionByIds(questionIds);
    }

    /**
     * 删除试题信息
     *
     * @param questionId 试题ID
     * @return 结果
     */
    @Override
    public int deleteSysQuestionById(Long questionId) {
        sysOptionMapper.deleteSysOptionById(questionId);
        return sysQuestionMapper.deleteSysQuestionById(questionId);
    }
}
